package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskInvoiceMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskInvoice;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskInvoiceServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskInvoiceMapper, TaskAutotaskInvoice>
        implements TaskAutotaskInvoiceService {

    @Override
    public void sync(ResultEntity content) {
        log.debug("Auto task invoice receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskInvoice data = TaskAutotaskInvoice.builder()
                .invoiceId(super.getLongValue(entity.get("id")))
                .batchId(ObjectUtils.isEmpty(entity.get("batchID")) ? null : entity.get("batchID").asInt())
                .invoiceEditorTemplateId(ObjectUtils.isEmpty(entity.get("invoiceEditorTemplateID")) ? null : entity.get("invoiceEditorTemplateID").asInt())
                .paymentTerm(ObjectUtils.isEmpty(entity.get("paymentTerm")) ? null : entity.get("paymentTerm").asInt())
                .taxGroup(ObjectUtils.isEmpty(entity.get("taxGroup")) ? null : entity.get("taxGroup").asInt())
                .comments(ObjectUtils.isEmpty(entity.get("comments")) ? null : entity.get("comments").asText())
                .invoiceNumber(ObjectUtils.isEmpty(entity.get("invoiceNumber")) ? null : entity.get("invoiceNumber").asText())
                .orderNumber(ObjectUtils.isEmpty(entity.get("orderNumber")) ? null : entity.get("orderNumber").asText())
                .taxRegionName(ObjectUtils.isEmpty(entity.get("taxRegionName")) ? null : entity.get("taxRegionName").asText())
                .invoiceTotal(ObjectUtils.isEmpty(entity.get("invoiceTotal")) ? null : entity.get("invoiceTotal").asDouble())
                .totalTaxValue(ObjectUtils.isEmpty(entity.get("totalTaxValue")) ? null : entity.get("totalTaxValue").asDouble())
                .isVoided(ObjectUtils.isEmpty(entity.get("isVoided")) ? null : entity.get("isVoided").asBoolean())
                .createDateTime(ObjectUtils.isEmpty(entity.get("createDateTime")) || "null".equalsIgnoreCase(entity.get("createDateTime").asText()) ? null:LocalDateTime.parse(entity.get("createDateTime").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .dueDate(ObjectUtils.isEmpty(entity.get("dueDate")) || "null".equalsIgnoreCase(entity.get("dueDate").asText()) ? null:LocalDateTime.parse(entity.get("dueDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .fromDate(ObjectUtils.isEmpty(entity.get("fromDate")) || "null".equalsIgnoreCase(entity.get("fromDate").asText()) ? null:LocalDateTime.parse(entity.get("fromDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .invoiceDateTime(ObjectUtils.isEmpty(entity.get("invoiceDateTime")) || "null".equalsIgnoreCase(entity.get("invoiceDateTime").asText()) ? null:LocalDateTime.parse(entity.get("invoiceDateTime").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .paidDate(ObjectUtils.isEmpty(entity.get("paidDate")) || "null".equalsIgnoreCase(entity.get("paidDate").asText()) ? null:LocalDateTime.parse(entity.get("paidDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .toDate(ObjectUtils.isEmpty(entity.get("toDate")) || "null".equalsIgnoreCase(entity.get("toDate").asText()) ? null:LocalDateTime.parse(entity.get("toDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .voidedDate(ObjectUtils.isEmpty(entity.get("voidedDate")) || "null".equalsIgnoreCase(entity.get("voidedDate").asText()) ? null:LocalDateTime.parse(entity.get("voidedDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .webServiceDate(ObjectUtils.isEmpty(entity.get("webServiceDate")) || "null".equalsIgnoreCase(entity.get("webServiceDate").asText()) ? null:LocalDateTime.parse(entity.get("webServiceDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskInvoice dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskInvoice>lambdaQuery()
                .eq(TaskAutotaskInvoice::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskInvoice::getInvoiceId, data.getInvoiceId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }

    }
}
